package com.sy.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sy.dao.AdminDao;
import com.sy.dao.NewsDao;
import com.sy.dao.impl.AdminDaoImpl;
import com.sy.dao.impl.NewsDaoImpl;
import com.sy.dao.impl.PageCount;
import com.sy.vo.Admin;
import com.sy.vo.News;

public class AdminAction extends ActionSupport {

	private static final long serialVersionUID = -3699334709726488611L;
	int i=1;//中间变量
	private int k;//储存最大页面数
	private int pageNow=1; //页码数,初始为1
	private int pageSize = 8 ; //页面行数 
	private int intRowCount;//总行数
	private int intPageCount;//总页数
	private List<News> Newss;
	private Admin admin;
	private String aname;
	private String apassword;
	public String getAname() {
		return aname;
	}
	public void setAname(String aname) {
		this.aname = aname;
	}
	public String getApassword() {
		return apassword;
	}
	public void setApassword(String apassword) {
		this.apassword = apassword;
	}
	public List<News> getNewss() {
		return Newss;
	}
	public void setNewss(List<News> newss) {
		Newss = newss;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public int getPageNow() {
		return pageNow;
	}
	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getK() {
		return k;
	}
	public void setK(int k) {
		this.k = k;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	@SuppressWarnings("unchecked")
//验证登陆
	public String execute() throws Exception {
		
		AdminDao adi=new AdminDaoImpl();
		admin.getAname();
		admin.getApassword();
		if(adi.isLogin(admin)){
			
			Map map=ActionContext.getContext().getSession();//插入Session的值
			map.put("user", admin.getAname());
			
			PageCount pc=new PageCount();
			String tableName="struts2new";
			intRowCount=pc.count(tableName);
			k=(intRowCount + pageSize - 1) / pageSize;
			intPageCount = (intRowCount + pageSize - 1) / pageSize;//计算出总页数
			if(pageNow<1){
				pageNow=1;
			}
			
			if(pageNow > intPageCount)
				 pageNow=intPageCount;
				 i = (pageNow -1)*pageSize;
			NewsDao nlist=new NewsDaoImpl();
			if(null!=nlist.queryByPage(i,pageSize)){
			Newss = nlist.queryByPage(i,pageSize);
			
			return SUCCESS;
			}else{
				return "failure";
			}
				}else
					return "failure";
		}
//添加管理员
public String add() throws Exception {

	String a=aname;
	String b=apassword;
	AdminDao aadd=new AdminDaoImpl();
	if(aadd.add(a,b) == 0){
		return "failure";
	}else{
	return SUCCESS;
	}
}
//退出登陆
@SuppressWarnings("unchecked")
public String out() throws Exception {

	Map map=ActionContext.getContext().getSession();//清空Session的值
	map.put("user", null);
	if(map.get("user")==null){
	return SUCCESS;
	}else{
		return "failure";
	}
	}
}